//
//  JHSUsersDetailViewController.m
//  LogIn(登录)
//
//  Created by chenWei on 2017/3/23.
//  Copyright © 2017年 陈伟. All rights reserved.
//

#import "JHSUsersDetailViewController.h"
#import "JHSUsersDetailHeaderView.h"

@interface JHSUsersDetailViewController ()<UIActionSheetDelegate>

//保存信息的数组
@property (nonatomic, strong) NSArray *arr;

@property (nonatomic, strong) NSDictionary *msgDict;


@property (weak, nonatomic) IBOutlet UILabel *nameLab;

@property (weak, nonatomic) IBOutlet UILabel *ageLab;

@property (weak, nonatomic) IBOutlet UILabel *genderLab;

@property (weak, nonatomic) IBOutlet UILabel *heightLab;

@property (weak, nonatomic) IBOutlet UILabel *weightLab;

@property (weak, nonatomic) IBOutlet UILabel *nativeLab;

@property (weak, nonatomic) IBOutlet UILabel *livePlaceLab;

@property (weak, nonatomic) IBOutlet UILabel *phoneNumLab;

@property (weak, nonatomic) IBOutlet UILabel *idLab;


/** 模型数组 */
@property(nonatomic,strong)NSMutableArray  *arrList;

//模态View
@property(nonatomic,strong)UIView  *modealView;
//数据
@property(nonatomic,copy)NSString *label;

//数据2
@property(nonatomic,copy)NSString *city;

//UItableView
@property(nonatomic,weak)UITableView  *tabView;

//相应的行和组
@property(nonatomic,assign)NSInteger  section;
@property(nonatomic,assign)NSInteger  row;
//小头像
@property(nonatomic,strong)UIButton *icon;

////当前用户的模型
//@property (nonatomic,strong)DBJLoginModel *model;



@end

@implementation JHSUsersDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self addData];
    
//    [self loadData];
    
    
}

//MARK:1- 创建及添加子控件
- (void)setupUI
{
    self.navigationController.navigationBarHidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    //头部图片视图
    UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1429862008539"]];
    imgV.frame = CGRectMake(0, 0, self.view.bounds.size.width, 200);
    
    //添加头像视图
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"patient_defaultphoto_male"]];
    img.layer.cornerRadius = 30;
    img.layer.masksToBounds = YES;
    
    [imgV addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(imgV.mas_bottom).offset(-10);
        make.centerX.equalTo(imgV.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    
    self.tableView.tableHeaderView = imgV;
    
    //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

//MARK:2- 添加数据
- (void)addData
{
    //左侧信息数组
    NSArray *arr = @[@"姓名",@"年龄",@"性别",@"身高",@"体重",@"籍贯",@"常居地",@"联系方式",@"证件号码"];
    
    _arr = arr;

    
    
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    self.section = indexPath.section;
    self.row = indexPath.row;
    switch (indexPath.row) {
        case 0:{
            //修改姓名
            [self updateCellWith:@"姓名"];
        }
            break;
        case 1:{
            //修改年龄
            [self updateCellWith:@"年龄"];
        }
            break;
        case 2:{
            //修改性别
            [self updateCellWith:@"性别"];
        }
            break;
        case 3:{
            //修改身高
            [self updateCellWith:@"身高"];
        }
            break;
        case 4:{
            //修改体重
            [self updateCellWith:@"体重"];
        }
            break;
        case 5:{
            //修改籍贯
            //修改常居地
            [self updateNative];
        }
            break;
        case 6:{
            //修改常居地
            [self updateNative];
        }
            break;
        case 7:{
            //修改联系方式
            [self updateCellWith:@"联系方式"];
        }
            
            break;
        case 8:{
            
            //修改证件号码
            [self updateCellWith:@"证件号码"];
        }
            
            break;
            
        default:
            break;
    }
    
    
    
    
    
}

/** 修改籍贯／常居地的方法 */
- (void)updateNative{
    [self.modealView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(self.view);
        
    }];
    
    //创建代码块
    void(^labelBlock)() = ^(NSString *label){
        
        NSDictionary *model = _arrList[self.row];
        NSDictionary *newModel = @{[model allKeys].lastObject : label};
        
        NSString *key = [newModel allKeys].lastObject;
        id value = [NSString stringWithFormat:@"%@",newModel[key]];
        [self updateInfo:value];
        [_arrList replaceObjectAtIndex:self.row withObject:newModel];
        
        //3.刷新tableView
        NSIndexPath *idxPath = [NSIndexPath indexPathForRow:self.row inSection:0];
        //局部刷新,刷新指定的行
        [self.tabView  reloadRowsAtIndexPaths:@[idxPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
        //4.更新modealView的frame
        [self.modealView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.mas_equalTo(self.view).offset(250);
            
        }];
        
        [UIView  animateWithDuration:0.25 animations:^{
            [self.modealView layoutIfNeeded];
        }];
        
    };
    //传递代码块
//    _footView.labelBlock =labelBlock;
}

/** 修改个人信息的方法 */
- (void)updateCellWith:(NSString*)title {
    //创建一个对话框
    UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    //修改UIAlertView样式，显示出一个文本框
    alterView.alertViewStyle = UIAlertViewStylePlainTextInput;
    
    //获取UIAlertView的指定文本框，并且设置内容文字
    [alterView textFieldAtIndex:0].text = self.label;
    
    // NSLog(@"%@",self.label);
    //显示
    [alterView show];
    
}

#pragma mark - UIAlertView代理
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  

    
    //点击确定时
    if (buttonIndex == 1) {
        
        if (_arrList==nil) {
            return;
        }
        
        //更新数据
        //1.获取文本框当前内容
        NSString *text = [alertView textFieldAtIndex:0].text;
        
        //        NSLog(@"%@",text);
        //2.修改当前选中的模型
        NSDictionary *model = _arrList[self.row];
        NSDictionary *newModel = @{[model allKeys].lastObject : text};
        
        NSString *key = [newModel allKeys].lastObject;
        id value = [NSString stringWithFormat:@"%@",newModel[key]];
        [self updateInfo:value];
        
        [_arrList replaceObjectAtIndex:self.row withObject:newModel];
        
        //3.刷新tableView
        NSIndexPath *idxPath = [NSIndexPath indexPathForRow:self.row inSection:0];
        //局部刷新,刷新指定的行
        [self.tabView  reloadRowsAtIndexPaths:@[idxPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

#pragma mark - 暴力更新本地数据
- (void)updateInfo:(id)value{
    //暴力修改
    switch (self.row) {
        case 0:
            _model.true_name = value;
            break;
            
        case 1:
//            _model.age = value;
            break;
            
        case 2:
//            _model.dbj_gender = value;
            break;
            
        case 3:
//            _model.height = value;
            break;
            
        case 4:
//            _model.weight = value;
            break;
            
        case 5:
            _model.address = value;
            break;
            
        case 6:
//            _model.habitual_residence = value;
            break;
            
        case 7:
            _model.mobile_number = value;
            break;
            
        case 8:
            _model.card_number = value;
            break;
            
        default:
            break;
    }
    
//    [DBJTools updateCurrentIDModel:_model];
}












@end
